package com.artisan.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.artisan.model.Admin;
import com.artisan.model.Student;
import com.artisan.model.Teacher;
import com.artisan.util.StringUtil;

public class TeacherDao extends BaseDao {
	public boolean addTeacher(Teacher teacher) {
		String sql = "insert into s_teacher values(null,?,?,?,?,?) ";
		try {
			PreparedStatement preparedStatement = con.prepareStatement(sql);
			preparedStatement.setString(1, teacher.getName());
			preparedStatement.setString(2, teacher.getSex());
			preparedStatement.setString(3, teacher.getTitle());
			preparedStatement.setInt(4, teacher.getAge());
			preparedStatement.setString(5, teacher.getPassword());
			if (preparedStatement.executeUpdate() > 0) {
				return true;
			}
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return false;
	}

	public List<Teacher> getTeacherList(Teacher teacher) {
		// TODO 自动生成的方法存根
		List<Teacher> retList = new ArrayList<Teacher>();
		StringBuffer sqlString = new StringBuffer("select * from s_teacher");
		if (!StringUtil.isEmpty(teacher.getName())) {
			sqlString.append(" where name like '%" + teacher.getName() + "%'");
		}
		try {
			PreparedStatement preparedStatement = con.prepareStatement(sqlString.toString());
			ResultSet executeQuery = preparedStatement.executeQuery();
			while (executeQuery.next()) {
				Teacher t = new Teacher();
				t.setId(executeQuery.getInt("id"));
				t.setName(executeQuery.getString("name"));
				t.setSex(executeQuery.getString("sex"));
				t.setTitle(executeQuery.getString("title"));
				t.setAge(executeQuery.getInt("age"));
				t.setPassword(executeQuery.getString("password"));
				retList.add(t);
			}
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return retList;
	}

	public boolean delete(int id) {
		String sql = "delete from s_teacher where id = ?";
		try {
			PreparedStatement preparedStatement = con.prepareStatement(sql);
			preparedStatement.setInt(1, id);
			if (preparedStatement.executeUpdate() > 0) {
				return true;
			}
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return false;
	}

	public boolean update(Teacher teacher) {
		String sql = "update s_teacher set name = ? ,sex = ? ,title = ? ,age = ? ,password = ? where id = ?";
		try {
			PreparedStatement preparedStatement = con.prepareStatement(sql);
			preparedStatement.setString(1, teacher.getName());
			preparedStatement.setString(2, teacher.getSex());
			preparedStatement.setString(3, teacher.getTitle());
			preparedStatement.setInt(4, teacher.getAge());
			preparedStatement.setString(5, teacher.getPassword());
			preparedStatement.setInt(6, teacher.getId());
			if (preparedStatement.executeUpdate() > 0) {
				return true;
			}
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return false;
	}

	public Teacher login(Teacher teacher) {
		String sql = "select * from s_teacher where name = ? and password = ?";
		Teacher teacherRet = null;
		try {
			PreparedStatement prst = con.prepareStatement(sql); // 把sql语句传给数据库操作对象
			prst.setString(1, teacher.getName());
			prst.setString(2, teacher.getPassword());
			ResultSet executeQurey = prst.executeQuery();
			if (executeQurey.next()) {
				teacherRet = new Teacher();
				teacherRet.setId(executeQurey.getInt("id"));
				teacherRet.setName(executeQurey.getString("name"));
				teacherRet.setPassword(executeQurey.getString("password"));
				teacherRet.setSex(executeQurey.getString("sex"));
				teacherRet.setAge(executeQurey.getInt("age"));
				teacherRet.setTitle(executeQurey.getString("title"));
			}
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return teacherRet;
	}

	public String editPassword(Teacher teacher, String newPassword) {
		String sql = "select * from s_teacher where id = ? and password = ?";
		PreparedStatement prst = null;
		int id = 0;
		try {
			prst = con.prepareStatement(sql);
			prst.setInt(1, teacher.getId());
			prst.setString(2, teacher.getPassword());
			ResultSet executeQurey = prst.executeQuery();
			if (!executeQurey.next()) {
				String retString = "旧密码错误!";
				return retString;
			}
			id = executeQurey.getInt("id");
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}

		String retString = "修改失败!";
		String sqlString = "update s_teacher set password = ? where id = ? ";
		try {
			prst = con.prepareStatement(sqlString);
			prst.setString(1, newPassword);
			prst.setInt(2, id);
			int ret = prst.executeUpdate();
			if (ret > 0) {
				retString = "密码修改成功!";
			}
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} // 把sql语句传给数据库操作对象
		return retString;
	}

	public boolean enrollTeacher(Teacher teacher) {
		String sql = "insert into s_teacher(id,name,password) values(null,?,?) ";
		try {
			PreparedStatement preparedStatement = con.prepareStatement(sql);
			preparedStatement.setString(1, teacher.getName());
			preparedStatement.setString(2, teacher.getPassword());
			if (preparedStatement.executeUpdate() > 0) {
				return true;
			}
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return false;
	}

	public boolean isTeacherSelected(Teacher teacher) {
		String sql = "select * from s_teacher where name = ? ";
		try {
			PreparedStatement prst = con.prepareStatement(sql);// 把sql语句传给数据库操作对象
			prst.setString(1, teacher.getName());
			ResultSet executeQuery = prst.executeQuery();
			if (executeQuery.next()) {
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
}
